package com.woniuxy.producer.config.delay;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitDelayConfig {
    // 配置一个延迟交换机
    @Bean
    public CustomExchange customExchange(){
        Map<String,Object> arguments = new HashMap<>();
        // 设置延迟交换机的类型参数：定向模式（路由模式）
        arguments.put("x-delayed-type","direct");
        //参数1：交换机名称；参数2：指定消息类型是延迟消息; 参数3：true表示交换机持久化; 参数
        //4:交换机没有队列绑定时候不自动删除参数5：交换机参数
        return new CustomExchange("delayed-exchange", "x-delayed-message",true,false,arguments);
    }
    // 延迟队列
    @Bean
    public Queue delayedQueue() {
        return new Queue("delayed-queue");
    }
    //队列绑定交换机
    @Bean
    public Binding bindingDelayedExchange(CustomExchange customExchange, Queue delayedQueue){
        return BindingBuilder.bind(delayedQueue).to(customExchange).with("msg.delay").noargs();
    }
}
